﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YJKTextDimension.DoorWindowDim
{
	public class DimensionTypeManager
	{
		private void InitDimTypeData()
		{
			List<DimensionType> dimensionTypeByStyleType = DimensionStyleManager.GetDimensionTypeByStyleType(0, DimensionTypeManager.m_doc);
			foreach (DimensionType dimensionType in dimensionTypeByStyleType)
			{
				string name = dimensionType.Name;
				if (!this.m_dictDimensionType.ContainsKey(name))
				{
					this.m_dictDimensionType.Add(name, dimensionType);
				}
				else
				{
					this.m_dictDimensionType[name] = dimensionType;
				}
			}
		}

		private void SetElementStringParamValue(Element elem, string sParamName, string sValue)
		{
			Parameter parameter = elem.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly)
			{
				string a = parameter.AsString();
				if (a != sValue)
				{
					parameter.Set(sValue);
				}
			}
		}

		private void SetElementDoubleParamValue(Element elem, string sParamName, double dValue)
		{
			Parameter parameter = elem.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly)
			{
				double num = parameter.AsDouble();
				if (num != dValue)
				{
					parameter.Set(dValue);
				}
			}
		}

		private void SetElementIntParam(Element elem, string sParamName, int nValue)
		{
			Parameter parameter = elem.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly)
			{
				int num = parameter.AsInteger();
				if (num != nValue)
				{
					parameter.Set(nValue);
				}
			}
		}

		private void SetElementIdParam(Element elem, string sParamName, ElementId id)
		{
			Parameter parameter = elem.GetParameter(sParamName);
			if (parameter != null && !parameter.IsReadOnly)
			{
				ElementId elementId = parameter.AsElementId();
				if (elementId != id && null != id)
				{
					parameter.Set(id);
				}
			}
		}

		public DimensionTypeManager(Autodesk.Revit.DB.Document doc)
		{
			DimensionTypeManager.m_doc = doc;
			this.InitDimTypeData();
		}

		public DimensionType CreateDimensionType(string sTypeName, DimensionParameter dimParameter)
		{
			DimensionType result = null;
			if (0 < this.m_dictDimensionType.Count && !string.IsNullOrEmpty(sTypeName))
			{
				if (this.m_dictDimensionType.Keys.Contains(sTypeName))
				{
					result = this.m_dictDimensionType[sTypeName];
				}
				else
				{
					DimensionType dimensionType = this.m_dictDimensionType.Values.First<DimensionType>();
					if (dimensionType != null)
					{
						DimensionType dimensionType2 = dimensionType.Duplicate(sTypeName) as DimensionType;
						this.SetElementIntParam(dimensionType2, "文本移动时显示引线", dimParameter.m_nLeadLine);
						this.SetElementIdParam(dimensionType2, "记号", dimParameter.m_idLead);
						this.SetElementIntParam(dimensionType2, "记号线宽", dimParameter.m_nLeadWidth);
						this.SetElementIntParam(dimensionType2, "尺寸界线控制点", dimParameter.m_nDimensionLineControlPoint);
						this.SetElementDoubleParamValue(dimensionType2, "尺寸界线长度", dimParameter.m_dDimensionLineLength);
						this.SetElementDoubleParamValue(dimensionType2, "尺寸界线延伸", dimParameter.m_dDimensionLineStretch);
						this.SetElementDoubleParamValue(dimensionType2, "尺寸标注线延长", dimParameter.m_dDimensionLineExtension);
						this.SetElementIntParam(dimensionType2, "颜色", dimParameter.m_nColor);
						this.SetElementDoubleParamValue(dimensionType2, "文字大小", dimParameter.m_dTextSize);
						this.SetElementIntParam(dimensionType2, "文字背景", dimParameter.m_nTextBackground);
						this.SetElementDoubleParamValue(dimensionType2, "文字偏移", dimParameter.m_dTextOffset);
						this.SetElementStringParamValue(dimensionType2, "文字字体", dimParameter.m_strTextFont);
						result = dimensionType2;
					}
				}
			}
			return result;
		}

		public static ElementId GetDimLeaderHeadTypeId(string sTypeName)
		{
			ElementId result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(DimensionTypeManager.m_doc);
            filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.ElementType));
			IEnumerable<Element> source = from element in filteredElementCollector
			where element.Name.Equals(sTypeName)
			select element;
			if (source.Count<Element>() > 0)
			{
				Element element2 = source.First<Element>();
				if (element2 != null)
				{
					result = element2.Id;
				}
			}
			return result;
		}

		public static Document m_doc;

		private Dictionary<string, DimensionType> m_dictDimensionType = new Dictionary<string, DimensionType>();
	}
}
